

// source idl: com.erp.gis.ao.idl.GisAo.java

#ifndef GISAO_STUB_4_WEB_H
#define GISAO_STUB_4_WEB_H

#include <string>
#include <sstream>
#include "app_platform/cmd_obj.h"
#include "app_platform/cgi_stub_cntl.h"
#include "c2cent/base/basestub4web.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_serializable.h"
#include "app_platform/intf_service_object.h"
#include "erp/gis/po/regionpo_gis.h"	
#include <set>	
#include <map>	
#include "erp/gis/po/ipaddresspo_gis.h"	
#include "intf_gisao.h"

namespace erp
{
namespace gis
{
namespace ao
{
	#define GISAO_STUB4WEB_VERSION "ERP_GIS_AO___GISAO__STUB4WEB_VERSION__1.0 2013-08-15 11:06:15"
    const std::string GISAO_STUB4WEB_NOUSE = GISAO_STUB4WEB_VERSION;

	const uint32_t GISAO_GETIPADDRESSBYIP_REQ_CMD_ID = 0xC66A1801; /// 请求命令号
	const uint32_t GISAO_GETIPADDRESSBYIP_RESP_CMD_ID = 0xC66A8801; /// 回应命令号
	

	const uint32_t GISAO_GETSMALLESTREGIONBYIP_REQ_CMD_ID = 0xC66A1802; /// 请求命令号
	const uint32_t GISAO_GETSMALLESTREGIONBYIP_RESP_CMD_ID = 0xC66A8802; /// 回应命令号
	


	const uint16_t GISAO_MAX_PROTOCAL_VERSION = 2; //>平台支持的最大协议版本号

///////////////////////////// STRUCTS /////////////////////////////////////////////////
	/**
	 *
 	 * 
	 *
	 *
 	 * @date 2013-08-15 11:06:15
 	 * @version 0
	 */	
	struct CGetIpAddressByIpReq : public IServiceObject, public ISerializableXML
	{
		std::string machineKey;
		std::string source;
		uint32_t sceneId;
		std::set<uint32_t > ipList;
		std::string inReserve;
		std::string errmsg;
		std::map<uint32_t,erp::gis::po::CIpAddressPo > ipAddress;
		std::string outReserve;
		
	    virtual uint32_t getCmdId() const { return GISAO_GETIPADDRESSBYIP_REQ_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
			//检查协议的版本号
		    //if(bs.getVersion() > GISAO_MAX_PROTOCAL_VERSION)
		    //return false;
			bs & machineKey;
			bs & source;
			bs & sceneId;
			bs & ipList;
			bs & inReserve;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	
	/**
	 *
 	 * 
	 *
 	 * @date 2013-08-15 11:06:15
 	 * @version 0
	 */	
	struct CGetIpAddressByIpResp : public IServiceObject, public ISerializableXML
	{
		uint32_t dwResult;
		std::string errmsg;
		std::map<uint32_t,erp::gis::po::CIpAddressPo > ipAddress;
		std::string outReserve;
	
	    virtual uint32_t getCmdId() const { return GISAO_GETIPADDRESSBYIP_RESP_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
		    bs & dwResult;
			bs & errmsg;
			bs & ipAddress;
			bs & outReserve;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	/**
	 *
 	 * 
	 *
	 *
 	 * @date 2013-08-15 11:06:15
 	 * @version 0
	 */	
	struct CGetSmallestRegionByIpReq : public IServiceObject, public ISerializableXML
	{
		std::string machineKey;
		std::string source;
		uint32_t sceneId;
		std::set<uint32_t > ipList;
		std::string inReserve;
		std::string errmsg;
		std::map<uint32_t,std::vector<erp::gis::po::CRegionPo > > smallestRegion;
		std::string outReserve;
		
	    virtual uint32_t getCmdId() const { return GISAO_GETSMALLESTREGIONBYIP_REQ_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
			//检查协议的版本号
		    //if(bs.getVersion() > GISAO_MAX_PROTOCAL_VERSION)
		    //return false;
			bs & machineKey;
			bs & source;
			bs & sceneId;
			bs & ipList;
			bs & inReserve;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	
	/**
	 *
 	 * 
	 *
 	 * @date 2013-08-15 11:06:15
 	 * @version 0
	 */	
	struct CGetSmallestRegionByIpResp : public IServiceObject, public ISerializableXML
	{
		uint32_t dwResult;
		std::string errmsg;
		std::map<uint32_t,std::vector<erp::gis::po::CRegionPo > > smallestRegion;
		std::string outReserve;
	
	    virtual uint32_t getCmdId() const { return GISAO_GETSMALLESTREGIONBYIP_RESP_CMD_ID; }
		virtual bool Serialize(CByteStreamNetwork& bs)
		{
		    bs & dwResult;
			bs & errmsg;
			bs & smallestRegion;
			bs & outReserve;
		    return bs.isGood();
		}
	    virtual void Dump(std::ostream& os)	{}
	
		virtual bool FromXML(const std::string& sXMLContent) { return true; }
		virtual bool ToXML(std::string& sXMLContent) { return true; }
	};
	class CGisAoStub4Web : public c2cent::CBaseStub4Web, public IGisAo	{
		public:
			uint32_t GetIpAddressByIp(
				const CCntlInfo& rCntlInfo,

				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const std::set<uint32_t >& ipList,
				const std::string& inReserve,
				std::string& errmsg,
				std::map<uint32_t,erp::gis::po::CIpAddressPo >& ipAddress,
				std::string& outReserve
			);
			uint32_t GetSmallestRegionByIp(
				const CCntlInfo& rCntlInfo,

				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const std::set<uint32_t >& ipList,
				const std::string& inReserve,
				std::string& errmsg,
				std::map<uint32_t,std::vector<erp::gis::po::CRegionPo > >& smallestRegion,
				std::string& outReserve
			);
	};
	
	/**
	 *
 	 *  根据IP获取地址信息 
	 *
		 *@param String machineKey: 机器码，必填  				
		 *@param String source: 调用来源，必填  				
		 *@param uint32_t sceneId: 场景id，必填  				
		 *@param Set ipList: IP列表，必填  				
		 *@param String inReserve: 请求保留字  				
		 *@param String errmsg: 错误信息  		
		 *@param Map ipAddress: 地址信息  		
		 *@param String outReserve: 返回保留字   		
     *
     *@return uint32_t : 0(成功) 其它失败
	 */	
	inline uint32_t CGisAoStub4Web::GetIpAddressByIp(
				const CCntlInfo& rCntlInfo,

				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const std::set<uint32_t >& ipList,
				const std::string& inReserve,
				std::string& errmsg,
				std::map<uint32_t,erp::gis::po::CIpAddressPo >& ipAddress,
				std::string& outReserve
	)
	{
		 CGetIpAddressByIpReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.ipList = ipList;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.ipAddress = ipAddress;
			oReq.outReserve = outReserve;
		 
		// CCGI_STUB_CNTL->setRouteKey(uint32_t routeKey); 没有设置routeKey  
		
	    CCGI_STUB_CNTL->setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			CCGI_STUB_CNTL->setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
		if((iRetCode = CCGI_STUB_CNTL->putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed: ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(GISAO_GETIPADDRESSBYIP_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << GISAO_GETIPADDRESSBYIP_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CGetIpAddressByIpResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
		    
			errmsg  = oResp.errmsg;
			ipAddress  = oResp.ipAddress;
			outReserve  = oResp.outReserve;
		    
		return oResp.dwResult;
	}
	/**
	 *
 	 *  根据IP获取地区列表信息，由高到低 
	 *
		 *@param String machineKey: 机器码，必填  				
		 *@param String source: 调用来源，必填  				
		 *@param uint32_t sceneId: 场景id，必填  				
		 *@param Set ipList: IP列表，必填  				
		 *@param String inReserve: 请求保留字  				
		 *@param String errmsg: 错误信息  		
		 *@param Map smallestRegion: 地址信息  		
		 *@param String outReserve: 返回保留字   		
     *
     *@return uint32_t : 0(成功) 其它失败
	 */	
	inline uint32_t CGisAoStub4Web::GetSmallestRegionByIp(
				const CCntlInfo& rCntlInfo,

				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const std::set<uint32_t >& ipList,
				const std::string& inReserve,
				std::string& errmsg,
				std::map<uint32_t,std::vector<erp::gis::po::CRegionPo > >& smallestRegion,
				std::string& outReserve
	)
	{
		 CGetSmallestRegionByIpReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.ipList = ipList;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.smallestRegion = smallestRegion;
			oReq.outReserve = outReserve;
		 
		// CCGI_STUB_CNTL->setRouteKey(uint32_t routeKey); 没有设置routeKey  
		
	    CCGI_STUB_CNTL->setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			CCGI_STUB_CNTL->setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
		if((iRetCode = CCGI_STUB_CNTL->putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed: ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(GISAO_GETSMALLESTREGIONBYIP_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << GISAO_GETSMALLESTREGIONBYIP_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CGetSmallestRegionByIpResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
		    
			errmsg  = oResp.errmsg;
			smallestRegion  = oResp.smallestRegion;
			outReserve  = oResp.outReserve;
		    
		return oResp.dwResult;
	}
		
}
}
}

#endif /* GISAO_STUB_4_WEB_H */
